
#include "SDL_config.h"
#ifdef __ANDROID__

/* Include the SDL main definition header */
#include "SDL_main.h"
/*******************************************************************************
                 Functions called by JNI
*******************************************************************************/
#include <jni.h>
/* Called before SDL_main() to initialize JNI bindings in SDL library */
extern void SDL_Android_Init(JNIEnv* env, jclass cls);

#include <android/log.h>

#define LOG_TAG "org.app.sdl"

#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

/* Start up the SDL app */
void Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject obj)
{
    /* This interface could expand with ABI negotiation, calbacks, etc. */
    SDL_Android_Init(env, cls);

    SDL_SetMainReady();

    /* Run the application code! */
    int status;
    char *argv[2];
    argv[0] = SDL_strdup("mythkast.sdl2");
    argv[1] = NULL;
    status = SDL_main(1, argv);

    /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
    /* exit(status); */
}

int Java_org_libsdl_app_MythHardwareDecoder_InitalRtmpLink(JNIEnv* env, jobject this, jstring uri)
{
	//ip2 = (char *) (*env)->GetStringUTFChars(env, ip, NULL);
	LOGE("start inital rtmp uri");
	const char* _uri = (const char *) (*env)->GetStringUTFChars(env, uri, NULL);
	LOGE("uri=%s",_uri);
	int ret = InitalRtmpLink(_uri);
	(*env)->ReleaseStringUTFChars(env, uri, _uri);
	return ret;
}

int Java_org_libsdl_app_MythHardwareDecoder_InitalP2PLink(JNIEnv* env, jobject this, int userid,jstring uri)
{
	//ip2 = (char *) (*env)->GetStringUTFChars(env, ip, NULL);
	LOGE("start inital p2p uri");
	const char* _uri = (const char *) (*env)->GetStringUTFChars(env, uri, NULL);
	LOGE("uri=%s",_uri);
	int ret = InitalP2PLink(userid,_uri);
	(*env)->ReleaseStringUTFChars(env, uri, _uri);
	return ret;
}

int Java_org_libsdl_app_MythHardwareDecoder_GetRtmpBuf(JNIEnv* env, jobject this, jbyteArray buffer, int len)
{
	char* buf;
	int ret = GetRtmpBuf(&buf);
	if(ret > 0){
		(*env)->SetByteArrayRegion(env,buffer, 0, ret, (jbyte*)buf);
		return ret;
	}else{
		return 0;
	}
}


int Java_org_libsdl_app_MythHardwareDecoder_CloseRtmpLink(JNIEnv* env, jobject this)
{
	return CloseRtmpLink();
}
#endif /* __ANDROID__ */

/* vi: set ts=4 sw=4 expandtab: */
